<?php
  require_once(ROOT_PATH.'ty/database_conf.php');
  require_once(ROOT_PATH.'ty/DataBaseDao.php');
  class NewsService extends dataBaseDao{
      private $ROOT_CATE = 0;
      /*  
      *  读取首页显示的文章信息
      */
      public function getNewsShowToIndexGroupCat(){
          
          $catList = $this->getCategoryOfIndex();
          foreach($catList as $k=>$v){
              $catList[$k]['news_list'] = $this->getNewsByCat($v['cate_id'], 7);
          }
          
          $this->getDb()->close();
          return $catList;          
      }
      
      public function getNewsContentHtml($catList){
          $html = '';
          $index = 0;
          foreach($catList as $k=>$v){
              $html .= '<div class="news" ';
              if($index%2==1){
                $html .= 'style="padding-left:10px;"';                  
              }
              $html .= ' >';
              
              $html .= '<h3 class="category_title">';
              $html .= '<span style="font-size:14px;">'.$v['cate_title'].'</span>';
              $html .= '<a href="#"><span class="more1"></span></a>';
              $html .= '</h3><ul >';
              foreach($v['news_list'] as $fk=>$fv){
                  $html .= '<li><a style="border-bottom:1px solid #666666;">'.$fv['news_title'].'</a></li>';
              }
              $html .= '</ul></div>';
          }
          
          return $html;
      }
      
      public function saveNewsInfo($newsInfo){
          $sql = "insert into ty_news(news_title,news_content,category_id,create_date) ";
          $sql .= " values('".$newsInfo['news_title']."','".$newsInfo['news_content']."',".$newsInfo['category_id'].",now())";
          
          return $this->getDb()->query($sql);
      }
      
      public function updateNewsInfo($newsInfo){
          $sql = "update ty_news set ";
          foreach($newsInfo as $k=>$v){
              if($k == 'news_id') continue;
              $sql .= " $k='$v' ,";
          }
          
          $sql = substr($sql, 0, strlen($sql)-1);
          $sql .= " where news_id=".$newsInfo['news_id'];
          $this->getDb()->query($sql);
      }
      
      public function getCategoryOfIndex(){
          $sql = "select * from news_category where is_show_index=1 order by order_no asc ";
          return $this->getDb()->fetchRows($sql);
      }
      
      public function getCategoryByCateId($cateId){
          $sql = "select * from news_category where cate_id=".strval($cateId)." and flag=1 ";
          return $this->getDb()->fetchRow($sql);
      }
      
      public function getCategoryByParentId($parentId){
          $sql = "select * from news_category where parent_id=".strval($parentId)." and flag=1 order by order_no asc ";
          return $this->getDb()->fetchRows($sql);
      }
      
      public function getIndexMenu($menuCount = 5) {
      	$list = $this->getCategoryByParentId($this->ROOT_CATE);
      	return array_slice($list, 0, $menuCount);
      }
      
      public function getNewsByCat($catId, $limit = -1){
          if(empty($catId)){
              return false;
          }
          
          $sql = "select * from ty_news where category_id=$catId order by update_date desc ";
          if($limit > 0){
              $sql .= " limit 0,$limit";
          }
          return $this->getDb()->fetchRows($sql);
      }
      
      public function getNewsInfoByNewsId($newsId){
          if(empty($newsId)){
              return false;
          }
          
          $sql = "select * from ty_news where news_id=$newsId";
          $news = $this->getDb()->fetchRow($sql);
          if(!empty($news)){
              $news['news_content'] = stripslashes($news['news_content']);
          }
          
          return $news;
      }
      
       public function getAllNewsList(){
           require_once(ROOT_PATH.'ty/TyUtils.php');
           
          $sql = "select a.*,b.cate_title from ty_news a left join news_category b on b.cate_id=a.category_id ";
          $sql .= " order by a.category_id asc, a.create_date desc";
          $list = $this->getDb()->fetchRows($sql);
          foreach($list as $k=>$v){
              $list[$k]['short_content'] = TyUtils::unicodeSubStr(strip_tags($v['news_content']), 70);
          }
          
          return $list;
      }
      
  }
  
  
?>
